feat: NativeAOT-compatible JSON serialization via source generation#5
Conversation
Co-authored-by: nlogozzo <17648453+nlogozzo@users.noreply.github.com>
…e path construction) Co-authored-by: nlogozzo <17648453+nlogozzo@users.noreply.github.com>
|
@copilot I've updated the code - I want this library to always use source generated route instead of the non native-aot compatable apis. Please look at the changes and fix jsonfileservice tests. The tests should just use the sourcegen methods and the old cases should be removed. Rename the case00# |
…unused JsonOptions Co-authored-by: nlogozzo <17648453+nlogozzo@users.noreply.github.com>
Done in 6a62246. Removed Cases 001–008 (reflection-based API) from |
|
@copilot Seeing these warnings: Please make AOT compatible. |
Co-authored-by: nlogozzo <17648453+nlogozzo@users.noreply.github.com>
Fixed in fd543dd. All six converters now use AOT-safe primitives:
|
Reflection-based
System.Text.Jsonusage throughout the library prevents<PublishAot>true</PublishAot>publishing. This PR replaces all reflection-based serialization with source-generation-backed alternatives across all serialization sites.Changes
IJsonFileService/JsonFileServiceJsonTypeInfo<T>-based overloads forLoad,LoadAsync,Save,SaveAsync— the only API is now AOT-safe, usingnew T()(where T : new()) instead ofActivator.CreateInstance<T>()JsonSerializerOptionsstatic field and static constructorConsumers provide their source-generated context's type info:
UpdaterServiceUpdaterServiceJsonContext(JsonSerializerContextwithSnakeCaseLowernaming) forList<GitHubRelease>andGitHubReleaseAssetGitHubReleaseround-trip with direct manual mapping, eliminating the need to source-generate external Octokit typesGetFromJsonAsync<T>(url, JsonSerializerOptions)with theJsonTypeInfo<T>overloadJsonSerializerOptionsfieldObjectExtensionsDeepCopy(JsonTypeInfo<T>)overload alongside the existing (now annotated) reflection-based oneJSON Converters
NullToZeroIntConverter,NullToFalseBoolConverter,NullToTrueBoolConverter,NullToEmptyStringConverter: replacedJsonSerializer.Deserialize/SerializewithUtf8JsonReader/Utf8JsonWriterprimitives directly, eliminating IL2026/IL3050 AOT warningsNullToDefaultValueConverter<T>andNullToDefaultObjectConverter<T>: replaced reflection-basedJsonSerializercalls withoptions.GetTypeInfo(typeof(T))→ AOT-safeJsonTypeInfo<T>overloads; addedwhere T : new()constraint onNullToDefaultObjectConverter<T>📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.